package com.mt.pojo;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;

import java.util.List;

/**
 * @author Wang
 * @Description TODO 后期修改分页,保证分页参数的正确性
 * @date 2020/11/16-20:13
 */
public class PageInfo<T> implements IPage<T> {
    /**
     * list
     */
    private List<T> lists;
    /**
     * total count
     */
    private Integer totalCount = 0;
    /**
     * page size
     */
    private Integer pageSize = 20;
    /**
     * current page
     */
    private Integer currentPage = 0;
    /**
     * pageNo
     */
    private Integer pageNo;

    public PageInfo(Integer currentPage, Integer pageSize) {
        if (currentPage == null) {
            currentPage = 1;
        }
        this.pageNo = (currentPage - 1) * pageSize;
        this.pageSize = pageSize;
        this.currentPage = currentPage;
    }

    public Integer getStart() {
        return pageNo;
    }

    public void setStart(Integer start) {
        this.pageNo = start;
    }

    public Integer getTotalPage() {
        if (pageSize == null || pageSize == 0) {
            pageSize = 10;
        }
        if (this.totalCount % this.pageSize == 0) {
            return (this.totalCount / this.pageSize) == 0 ? 1 : (this.totalCount / this.pageSize);
        }
        return (this.totalCount / this.pageSize + 1);
    }

    public List<T> getLists() {
        return lists;
    }

    public void setLists(List<T> lists) {
        this.lists = lists;
    }

    public Integer getTotalCount() {
        if (totalCount == null) {
            totalCount = 0;
        }
        return totalCount;
    }

    public void setTotalCount(Integer totalCount) {
        this.totalCount = totalCount;
    }

    public Integer getPageSize() {
        if (pageSize == null || pageSize == 0) {
            pageSize = 10;
        }
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public void setCurrentPage(Integer currentPage) {
        this.currentPage = currentPage;
    }

    public Integer getCurrentPage() {
        if (currentPage == null || currentPage <= 0) {
            this.currentPage = 1;
        }
        return this.currentPage;
    }


    @Override
    public List<OrderItem> orders() {
        return null;
    }

    @Override
    public List<T> getRecords() {
        return this.lists;
    }

    @Override
    public IPage<T> setRecords(List<T> records) {
        this.lists=records;
        return this;
    }

    @Override
    public long getTotal() {
        return this.totalCount;
    }

    @Override
    public IPage<T> setTotal(long total) {
        this.totalCount = Math.toIntExact(total);
        return this;
    }

    @Override
    public long getSize() {
        return this.pageSize;
    }

    @Override
    public IPage<T> setSize(long size) {
        this.pageSize= Math.toIntExact(size);
        return this;
    }

    @Override
    public long getCurrent() {
        return this.currentPage;
    }

    @Override
    public IPage<T> setCurrent(long current) {
        this.currentPage= Math.toIntExact(current);
        return this;
    }
}
